package com.weasing.res.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.weasing.res.dao.IWxAdminDao;
import com.weasing.res.dao.IWxEvalrecordDao;
import com.weasing.res.dao.IWxEvalresultDao;
import com.weasing.res.dao.IWxUserdetailDao;
import com.weasing.res.dao.IWxUserlistDao;
import com.weasing.res.domain.WxAdmin;
import com.weasing.res.domain.WxEvalresult;
import com.weasing.res.domain.WxUserdetail;
import com.weasing.res.domain.WxUserlist;
import com.weasing.res.service.IFileLoadService;
import com.weasing.res.service.IWxDictService;
import com.weasing.res.utils.DateUtils;
import com.weasing.res.utils.DicUtils;
import com.weasing.res.vo.CreatTimeVo;
import com.weasing.res.vo.FileLoadVO;

@Service
@Transactional
public class FileLoadServiceImpl
		implements IFileLoadService {
	@Autowired
	private IWxUserdetailDao wxUserdetailDao;
	@Autowired
	private IWxAdminDao wxAdminDao;
	@Autowired
	private IWxUserlistDao wxUserlistDao;
	@Autowired
	private IWxEvalresultDao wxEvalresultDao;// 总分统计---压根不需要这个表
	@Autowired
	private IWxEvalrecordDao wxEvalrecordDao; // 分段统计
	@Autowired
	private IWxDictService wxDictService;

	// 公用map 找字段用 ---用的时候记得先清理
	private Map<String, Object> map = new HashMap<String, Object>();

	/**
	 * 组装vo准备数据
	 * 
	 * @author 黄龙
	 */
	@Override
	public FileLoadVO findVOByDetail(Integer detailId) {
		FileLoadVO vo = new FileLoadVO();

		// 组装评估信息
		WxUserdetail wxUserdetail = wxUserdetailDao.findById(detailId);
		// 罪名信息
		String chargetype = wxDictService.findWxDictByDictName("chargetype");
		List<String> chargetypeList = DicUtils.string2List(chargetype);
		Integer charget = wxUserdetail.getChargetype() - 1;
		wxUserdetail.setChargetypeStr(chargetypeList.get(charget));

		// 组装嫌疑人信息
		WxUserlist wxUserlist = wxUserlistDao
				.findById(wxUserdetail.getUserid());

		// 出生年月信息----有可能为空
		Date birthday = wxUserlist.getBirthday();
		if (null != birthday) {
		String date2String = DateUtils.date2String(birthday);
		wxUserlist.setBirthdayStr(date2String);
		} else {
			wxUserlist.setBirthdayStr("");
		}

		// 查询名族信息---有可能为空
		String nation = wxDictService.findWxDictByDictName("nation");
		if(null != nation){
		List<String> string2List = DicUtils.string2List(nation);
		Integer nation2 = wxUserlist.getNation()-1;
		wxUserlist.setNationTypeStr(string2List.get(nation2));
		} else {
			wxUserlist.setNationTypeStr("");
		}
		
		// 因为表里面没有 操作人id的字段 得不到数据----所以放弃
		// // 组装人员案件集合信息
		// map.clear();
		// map.put("udetailid", detailId);
		// List<WxChecklist> wxChecklists = wxChecklistDao.findByField(map);
		// WxChecklist wxChecklist = wxChecklists.get(0);
		// WxAdmin wxAdmin = wxAdminDao.findById(wxChecklist.getFadminid());

		// 组装评分信息
		map.clear();
		map.put("udetailid", detailId);
		List<WxEvalresult> findByField = wxEvalresultDao.findByField(map);
		WxEvalresult wxEvalresult = findByField.get(0);
		Integer totalscore = wxEvalresult.getTotalscore();
		// 二,找分段表的分数 返回的是list<Object[]>
		List findByDetail = wxEvalrecordDao.findByDetail(detailId);
		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < findByDetail.size(); i++) {
			Object[] obj = (Object[]) findByDetail.get(i);
			sb.append(obj[0] + "  " + obj[1] + "分"
					+ System.getProperty("line.separator"));
		}
		

		sb.append("总分: " + totalscore + System.getProperty("line.separator"));
		sb.append("评论结果: ");
		sb.append(totalscore > -6 ? "羁押" : "不予羁押");
		
		
		// 管理 安排一个假数据 adminid=33
		WxAdmin wxAdmin = wxAdminDao.findById(33);

		vo.setWxAdmin(wxAdmin);
		vo.setTolQues(sb.toString());
		System.out.println(vo.getTolQues());
		vo.setWxUserdetail(wxUserdetail);
		vo.setWxUserlist(wxUserlist);
		vo.setDetain(totalscore > -6);

		return vo;
	}

	/**
	 * 羁押表的数据准备
	 * 
	 * @author 黄龙
	 * 
	 */
	public FileLoadVO findVOForJiya(Integer detailId) {
		FileLoadVO vo = new FileLoadVO();

		// 组装评估信息
		WxUserdetail wxUserdetail = wxUserdetailDao.findById(detailId);
		// 罪名信息
		String chargetype = wxDictService.findWxDictByDictName("chargetype");
		List<String> chargetypeList = DicUtils.string2List(chargetype);
		Integer charget = wxUserdetail.getChargetype() - 1;
		wxUserdetail.setChargetypeStr(chargetypeList.get(charget));

		// 组装嫌疑人信息
		WxUserlist wxUserlist = wxUserlistDao
				.findById(wxUserdetail.getUserid());

		// 评估日期
		Timestamp createtime = wxUserlist.getCreatetime();
		int year = createtime.getYear();
		int month = createtime.getMonth();
		int day = createtime.getDay();
		CreatTimeVo time = new CreatTimeVo();
		time.setDay(day + "");
		time.setYear(year + "");
		time.setMonth(month + "");
		// 因为表里面没有 操作人id的字段 得不到数据----所以放弃
		// // 组装人员案件集合信息
		// map.clear();
		// map.put("udetailid", detailId);
		// List<WxChecklist> wxChecklists = wxChecklistDao.findByField(map);
		// WxChecklist wxChecklist = wxChecklists.get(0);
		// WxAdmin wxAdmin = wxAdminDao.findById(wxChecklist.getFadminid());

		// 组装评分信息
		List<WxEvalresult> findByField = wxEvalresultDao.findByField(map);
		WxEvalresult wxEvalresult = findByField.get(0);
		Integer totalscore = wxEvalresult.getTotalscore();


		// 管理 安排一个假数据 adminid=33
		WxAdmin wxAdmin = wxAdminDao.findById(33);

		vo.setWxAdmin(wxAdmin);
		vo.setWxUserdetail(wxUserdetail);
		vo.setWxUserlist(wxUserlist);
		vo.setDetain(totalscore > -6);
		vo.setCreatTime(time);
		vo.setNewdata(DateUtils.date2Chinese(new Date()));
		return vo;
	}

}
